/**
 * Created on 2004-10-9
 *
 * ��ݿ������
 */
package dbop;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import config.appconfig;


/**
 * @author PDV
 *
 * 
 */
public class DBManager {
    private static Logger logger = Logger.getLogger(DBManager.class);
    public static class DBConnParams {
    	
    	public final String dbType;
        public final String dbUrl;
    	public final String user;
        public final String password;
    	
        /**
         * @param sid
         * @param host
         * @param user
         * @param password
         * @param port
         */
        public DBConnParams(String type, String dbUrl, String user,
                String password) {
            super();
            this.dbType = type;
            this.dbUrl = dbUrl;
            this.user = user;
            this.password = password;
        }
        
        public static DBConnParams createDBConnParams(String dbName) {
            String s = dbName+'.';
            return new DBConnParams(appconfig.getStrProp(s+"type"), 
            		appconfig.getStrProp(s+"url"),
            		appconfig.getStrProp(s+"user"),
            		appconfig.getStrProp(s+"password")
                    );
        }
        
    }
    
    public static final String SQLSERVER = "SQLSERVER";
    
    public static final String ORACLE = "ORACLE";
    
    public static final String SQLSERVER2005 = "SQLSERVER2005";
    
    
    public static Connection generateDBConnection(DBConnParams param) throws SQLException {
        try {
	    	if (param.dbType.equals(ORACLE))
	    		Class.forName("oracle.jdbc.OracleDriver");
	    	else if (param.dbType.equals(SQLSERVER2005)) 
	    		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	else
	    		Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        } catch (ClassNotFoundException e) {
    		logger.fatal("jdbc class load error", e);
            System.exit(1);
        }
	    return DriverManager.getConnection(param.dbUrl, param.user, param.password);        
    }
    
    public static Connection generateDBConn() throws SQLException {
    	DBConnParams param = DBConnParams.createDBConnParams("DB");
        return generateDBConnection(param);
    }
    
	public static boolean isDBDisconnectError(SQLException e) {
		DBConnParams param = DBConnParams.createDBConnParams("DB");
		int errorCode = e.getErrorCode();
		if (param.dbType.equals(ORACLE)) {
	    	if ( errorCode == 17002 || errorCode == 17008 ) {
	    		return true;
	    	}
	    } else { //sqlserver
	        if (errorCode == 0)
	        	return true;
	    }
		return false;
	}
	public static boolean isDBDisconnectError(String name,SQLException e) {
		DBConnParams param = DBConnParams.createDBConnParams(name);
		int errorCode = e.getErrorCode();
		if (param.dbType.equals(ORACLE)) {
	    	if ( errorCode == 17002 || errorCode == 17008 ) {
	    		return true;
	    	}
	    } else { //sqlserver
	        if (errorCode == 0)
	        	return true;
	    }
		return false;
	}
	public static Connection generateDBConn(String name) throws SQLException {
    	DBConnParams param = DBConnParams.createDBConnParams(name);
        return generateDBConnection(param);
    }
    public static Connection getmysqlConn(String name)throws SQLException {
    	DBConnParams param = DBConnParams.createDBConnParams(name);
    	try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return DriverManager.getConnection(param.dbUrl, param.user, param.password);  
		//return DriverManager.getConnection(param.dbUrl, "xzlklcx", "bsth34");  
    }


}
